﻿using System;
using System.Collections.Generic;

namespace Muccu
{
    internal class Cache<TKey,TItem>
    {
        private Dictionary<TKey,TItem> _cache = new Dictionary<TKey, TItem>();
        private Func<TKey,TItem> _missing = key => default(TItem);
        private object _lock = new object();
        public Cache()
        {

        }
        public Cache(Func<TKey, TItem> missing)
        {
            _missing = missing;
        }

        public TItem Get(TKey name)
        {
            lock (_lock)
            {
                if (!_cache.ContainsKey(name))
                    _cache.Add(name, _missing(name));
            }
            return _cache[name];
        }
    }
}
